/**
 * Project Name:sfsp_util
 * File Name:ImageUtils.java
 * Package Name:com.hyht.util.common
 * Date:2017年6月19日上午9:42:40
 * Copyright (c) 2017 hangyunhuitong.com
 */
package com.wanlian.util;

import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.wanlian.File.FileUtils;
import com.wanlian.model.WaterMarkSettings;
import net.coobird.thumbnailator.Thumbnails;

/**
 * ClassName: ImageUtils <br/>
 * Function: TODO 图片处理. <br/>
 * Reason: TODO 水印加工、图片压缩. <br/>
 * date: 2017年6月19日 上午9:42:40 <br/>
 *
 * @author lenovo
 * @version 0.0.1-SNAPSHOT
 */
public class ImageUtils {
    private static String srcPath = null;

    private static String dstPath = null;

    private static String iconPath = null;

    static {
        String basePath = ImageUtils.class.getResource("/").getPath();
        srcPath = basePath + "file/src";
        dstPath = basePath + "file/dst";
        iconPath = basePath + "file/icon";
    }

    /**
     *
     * @Title: waterMarkByText
     * @Description: 图片添加水印文字
     * @param input 图片输入流
     * @param dstImgPath 存储目标位置
     * @param settings 水印设置
     * @throws IOException
     */
    public static void waterMarkByText(InputStream input, String dstImgPath, WaterMarkSettings settings) throws IOException {
        // 图片读人
        Image srcImg = ImageIO.read(input);

        // 图片长、宽
        int width = srcImg.getWidth(null);
        int height = srcImg.getHeight(null);

        // 位图
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // 画笔
        Graphics2D g = bufferedImage.createGraphics();

        // 设置对线段的锯齿状边缘处理
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

        // 画图步骤1-写入原图
        g.drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

        // 设置水印旋转
        g.rotate(Math.toRadians(settings.getDegree()), width / 2, height / 2);

        // 设置画笔颜色
        g.setColor(settings.getFontColor());

        // 设置混合度
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, settings.getAlpha()));

        // 设置字体
        Font font = new Font(settings.getFontType(), settings.getFontStyle(), settings.getFontSize());
        g.setFont(font);

        // 边界
        FontRenderContext context = g.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(settings.getLogoText(), context);

        // 水印位置
        float left = (float) ((width - bounds.getWidth()) / 2);
        float top = (float) ((height - bounds.getHeight()) / 2);

        // 画图步骤2-写入水印文字
        g.drawString(settings.getLogoText(), left, top);

        // 画笔销毁
        g.dispose();

        // 图片写出
        File dstFile = FileUtils.createNewFile(dstImgPath);
        ImageIO.write(bufferedImage, "JPG", dstFile);
    }

    /**
     *
     * @Title: waterMarkByText
     * @Description: 图片添加水印文字
     * @param input 图片输入流
     * @param dstImgPath 存储目标位置
     * @param settings 水印设置
     * @throws IOException
     */
    public static void waterMarkByIcon(InputStream input, String dstImgPath, WaterMarkSettings settings) throws IOException {
        // 图片读人
        Image srcImg = ImageIO.read(input);

        // 图片长、宽
        int width = srcImg.getWidth(null);
        int height = srcImg.getHeight(null);

        // 位图
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // 画笔
        Graphics2D g = bufferedImage.createGraphics();

        // 设置对线段的锯齿状边缘处理
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

        // 画图步骤1-写入原图
        g.drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

        // 设置水印旋转
        g.rotate(Math.toRadians(settings.getDegree()), width / 2, height / 2);

        // 设置混合度
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, settings.getAlpha()));

        // 图标读入
        ImageIcon icon = new ImageIcon(settings.getIconPath());

        // 图片位置
        int left = (width - icon.getIconWidth()) / 2;
        int top = (height - icon.getIconHeight()) / 2;

        // 画图步骤2-写入图标
        g.drawImage(icon.getImage(), left, top, null);

        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

        // 释放资源
        g.dispose();

        // 图片写出
        File dstFile = FileUtils.createNewFile(dstImgPath);
        ImageIO.write(bufferedImage, "JPG", dstFile);
    }

    public static void testWaterMarkByText() {
        String srcImgPath = srcPath + "/landscape.jpg";
        String dstImgPath = dstPath + "/landscape.jpg";

        try {
            InputStream input = new FileInputStream(new File(srcImgPath));
            waterMarkByText(input, dstImgPath, new WaterMarkSettings());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void testWaterMarkByIcon() {
        String srcImgPath = srcPath + "/landscape.jpg";
        String dstImgPath = dstPath + "/landscape.jpg";
        String iconImgPath = iconPath + "/panda.png";

        try {
            WaterMarkSettings settings = new WaterMarkSettings();
            settings.setIconPath(iconImgPath);

            InputStream input = new FileInputStream(new File(srcImgPath));
            waterMarkByIcon(input, dstImgPath, settings);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void compressImage(String filePath){
        try {
            String newFileName = "C:\\holly7\\files\\upload_152\\"+Utils.getFilename(filePath);
            Thumbnails.of(filePath).size(300, 300).toFile(newFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void compressImage(String filePath,String dstPath){
        try {
            Thumbnails.of(filePath).size(300 ,300).toFile(dstPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void compressByZIP(String srcPath,String dstPath){
        try {
            ZIPImage.compressPic(srcPath,dstPath,true,1000,1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String srcPath="C:\\holly7\\files\\ios\\4b1d5303ff2a87072cf3834f7d70c5f2.jpg";
        String dstPath="C:\\holly7\\files\\ios\\test.jpg";
        compressByZIP(srcPath,dstPath);
    }
}
